﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using STS.Model;

namespace STS.ViewModel
{
    public class PoiViewModel : BaseViewModel
    {
        // All to-do items.
        private ObservableCollection<Poi> _poiItems;
        public ObservableCollection<Poi> PoiItems
        {
            get { return _poiItems; }
            set
            {
                _poiItems = value;
                NotifyPropertyChanged("PoiItems");
            }
        }

        // Query database and load the collections and list used by the pivot pages.
        public void LoadCollectionsFromDatabase()
        {

            // Specify the query for all to-do items in the database.
            var poiItemsInDB = from Poi poi in ModelInstance.DataContext.PoiItems
                               select poi;

            // Query the database and load all poi items.
            PoiItems = new ObservableCollection<Poi>(poiItemsInDB);
        }

        // Add a to-do item to the database and collections.
        public void AddPoiItem(Poi newPoiItem)
        {
            // Add a poi item to the data context.
            ModelInstance.DataContext.PoiItems.InsertOnSubmit(newPoiItem);

            // Save changes to the database.
            ModelInstance.DataContext.SubmitChanges();

            // Add a poi item to the "all" observable collection.
            PoiItems.Add(newPoiItem);
        }

        // Remove a to-do task item from the database and collections.
        public void DeletePoiItem(Poi poiForDelete)
        {

            // Remove the poi item from the "all" observable collection.
            PoiItems.Remove(poiForDelete);

            // Remove the poi item from the data context.
            ModelInstance.DataContext.PoiItems.DeleteOnSubmit(poiForDelete);

            // Save changes to the database.
            ModelInstance.DataContext.SubmitChanges();
        }

        public string GetPoiImage(int idPoi)
        {
            var poiDB = from poi in ModelInstance.DataContext.PoiItems
                        where poi.PoiId == idPoi
                        select poi;
            Poi tmpPoi = poiDB.SingleOrDefault<Poi>();
            if (tmpPoi != null)
            {
                return tmpPoi.Image;
            }

            return "";
        }
    }
}
